package com.xingda.packing.dmds.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmds.common.PackUtils;
import com.xingda.packing.dmds.minitor.entity.PkShuntArea;
import com.xingda.packing.dmds.minitor.entity.PkShuntWheel;
import com.xingda.packing.dmds.minitor.entity.PkWheel;
import com.xingda.packing.dmds.minitor.mapper.PkShuntAreaMapper;
import com.xingda.packing.dmds.minitor.mapper.PkShuntWheelMapper;
import com.xingda.packing.dmds.minitor.mapper.PkWheelMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Slf4j
@Component("shuntDMDSArea")
public class ShuntArea {
    @Resource
    private PkShuntAreaMapper pkShuntAreaMapper;
    @Resource
    private PkShuntWheelMapper pkShuntWheelMapper;
    @Resource
    private PkWheelMapper pkWheelMapper;

    public void addWheelToSql(Wheel wheel, int area,int location) {
        PkShuntWheel pkShuntWheel = PackUtils.toPkShuntWheel(wheel);
        pkShuntWheelMapper.insert(pkShuntWheel);
        PkShuntArea pkShuntArea = new PkShuntArea();
        pkShuntArea.setArea(area);
        pkShuntArea.setLocation(location);
        pkShuntArea.setBarCode(wheel.getBarCode());
        pkShuntArea.setWheelId(pkShuntWheel.getId());
        pkShuntArea.setCreateTime(new Date());
        pkShuntArea.setTag(1);
        pkShuntAreaMapper.insert(pkShuntArea);
    }

    public void takeWheelToSql(int area) {
        LambdaQueryWrapper<PkShuntArea> shuntAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shuntAreaLambdaQueryWrapper.eq(PkShuntArea::getTag, 1).eq(PkShuntArea::getArea, area);
        List<PkShuntArea> pkShuntAreas = pkShuntAreaMapper.selectList(shuntAreaLambdaQueryWrapper);
        if (!pkShuntAreas.isEmpty()) {
            List<String> collect = pkShuntAreas.stream().map(PkShuntArea::getBarCode).collect(Collectors.toList());
            LambdaQueryWrapper<PkWheel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(PkWheel::getBarCode, collect);
            List<PkWheel> pkWheels = pkWheelMapper.selectList(lambdaQueryWrapper);
            if (!pkWheels.isEmpty()) {
                List<String> collect1 = pkWheels.stream().map(PkWheel::getBarCode).collect(Collectors.toList());
                LambdaQueryWrapper<PkShuntArea> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.in(PkShuntArea::getBarCode, collect1);
                List<PkShuntArea> pkShuntAreas1 = pkShuntAreaMapper.selectList(lambdaQueryWrapper1);
                for (PkShuntArea pkShuntArea : pkShuntAreas1) {
                    pkShuntArea.setTag(0);
                    pkShuntAreaMapper.updateById(pkShuntArea);
                }
            }
            LambdaQueryWrapper<PkShuntArea> shuntAreaLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            shuntAreaLambdaQueryWrapper1.eq(PkShuntArea::getTag, 1).eq(PkShuntArea::getArea, area);
            List<PkShuntArea> pkShuntAreaList = pkShuntAreaMapper.selectList(shuntAreaLambdaQueryWrapper1);
            if (!pkShuntAreaList.isEmpty()) {
                PkShuntArea pkShuntArea = pkShuntAreas.get(0);
                pkShuntArea.setTag(0);
                pkShuntAreaMapper.updateById(pkShuntArea);
            }
        }
    }

    public boolean canPutToSql(int location,int area) {
        LambdaQueryWrapper<PkShuntArea> pkShuntAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pkShuntAreaLambdaQueryWrapper
                .eq(PkShuntArea::getArea, location)
                .eq(PkShuntArea::getTag, 1)
                .eq(PkShuntArea::getLocation, area);
        return pkShuntAreaMapper.selectList(pkShuntAreaLambdaQueryWrapper).size() < 5;
    }

    public void cleanAllToSql(int area,int location) {
        LambdaQueryWrapper<PkShuntArea> pkShuntAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pkShuntAreaLambdaQueryWrapper
                .eq(PkShuntArea::getArea, area)
                .eq(PkShuntArea::getTag, 1)
                .eq(PkShuntArea::getLocation, location);
        List<PkShuntArea> pkShuntAreas = pkShuntAreaMapper.selectList(pkShuntAreaLambdaQueryWrapper);
        if (!pkShuntAreas.isEmpty()) {
            for (PkShuntArea pkShuntArea : pkShuntAreas) {
                pkShuntArea.setTag(0);
                pkShuntAreaMapper.updateById(pkShuntArea);
            }
        }
    }
}
